package com.springboot.blog.controller.admin;

import com.springboot.blog.dto.LayPage;
import com.springboot.blog.dto.LayResponse;
import com.springboot.blog.entity.Comment;
import com.springboot.blog.service.ICommentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
class AdminCommentController {
    @Autowired
    ICommentService commentService;
    String PATH = "admin/comment/";

    @RequiresPermissions("comment:list")
    @GetMapping(value = {"/admin/comment/index", "/admin/comment"})
    public String index() {
        return PATH + "index";
    }

    @RequiresPermissions("comment:list")
    @GetMapping(value = {"/admin/comment/tree"})
    @ResponseBody
    public LayResponse tree(Integer page, Integer limit, Comment param) {
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        LayPage<Comment> pageComment = new LayPage<>(page, limit);
        commentService.page(pageComment);
        return pageComment.getLayResponse();
    }

    @RequiresPermissions("comment:delete")
    @PostMapping(value = {"/admin/comment/delete"})
    @ResponseBody
    public LayResponse delete(String id) {
        boolean result = commentService.removeById(id);
        LayResponse layResponse = new LayResponse();
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("comment:delete")
    @PostMapping(value = {"/admin/comment/deleteAll"})
    @ResponseBody
    public LayResponse deleteAll(@RequestParam("ids[]") List<Integer> ids) {
        boolean result = commentService.removeByIds(ids);
        LayResponse layResponse = new LayResponse();
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

}
